package com.kanshu.basictest.ui.activity;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import androidx.annotation.NonNull;
import android.util.Log;

import com.kanshu.basictest.R;
import com.kanshu.basictest.sys.permission.PermissionHelper;
import com.kanshu.basictest.sys.permission.PermissionCallback;
import com.kanshu.basictest.ui.base.BaseActivity;

import java.util.ArrayList;
import java.util.Arrays;

public class SplashActivity extends BaseActivity implements PermissionCallback {

    private static final String TAG = "SplashActivity";

    private PermissionHelper mPermissionHelper;

    //申请权限是的请求码
    private static final int PERMISSION_REQUEST_CODE = 100;

    //程序需要额外申请的权限
    private static final ArrayList<String> sPermissions = new ArrayList<>();

    static {
        sPermissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        sPermissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    @Override
    protected int provideLayoutResId() {
        return R.layout.activity_splash;
    }


    @Override
    protected void init() {
        super.init();
        mPermissionHelper = new PermissionHelper(this, PERMISSION_REQUEST_CODE, this);
        //申请权限
        requestPermissions();
    }

    private void requestPermissions() {
        mPermissionHelper.requestPermissions(sPermissions.toArray(new String[]{}));
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        Log.i(TAG, "onRequestPermissionsResult: permissions" + Arrays.toString(permissions));
        Log.i(TAG, "onRequestPermissionsResult: grantResults" + Arrays.toString(grantResults));

        mPermissionHelper.handlePermissionResult(requestCode, permissions, grantResults);
    }


    @Override
    public void onRequestPermissionSuccess() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onRequestPermissionFail() {
        showExitDialog("程序即将退出", "本应用所申请权限均为必要权限，您有权限尚未授权，暂时无法使用！");
    }


    private void showExitDialog(String title, String msg) {
        getDialogManager().createCommonDialog(title, msg, "确定", null, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
            }
        }, null).show();
    }

}
